﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework.Graphics;

namespace Terraria.ModLoader.IO
{
	// Token: 0x020002C2 RID: 706
	public class ImageIO
	{
		// Token: 0x06001BAE RID: 7086 RVA: 0x00431658 File Offset: 0x0042F858
		public static bool ToRaw(Stream src, Stream dst)
		{
			bool result;
			using (Bitmap bitmap = new Bitmap(src))
			{
				if (bitmap.Width > 2048 || bitmap.Height > 2048)
				{
					result = false;
				}
				else
				{
					BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
					int[] array = new int[bitmap.Width * bitmap.Height];
					Marshal.Copy(bitmapData.Scan0, array, 0, array.Length);
					BinaryWriter binaryWriter = new BinaryWriter(dst);
					binaryWriter.Write(1);
					binaryWriter.Write(bitmap.Width);
					binaryWriter.Write(bitmap.Height);
					foreach (int num in array)
					{
						int num2 = num >> 24 & 255;
						int num3 = num >> 16 & 255;
						int num4 = num >> 8 & 255;
						int num5 = num & 255;
						if (num2 == 0)
						{
							binaryWriter.Write(0);
						}
						else
						{
							binaryWriter.Write((byte)num3);
							binaryWriter.Write((byte)num4);
							binaryWriter.Write((byte)num5);
							binaryWriter.Write((byte)num2);
						}
					}
					result = true;
				}
			}
			return result;
		}

		// Token: 0x06001BAF RID: 7087 RVA: 0x004317A4 File Offset: 0x0042F9A4
		public static byte[] ToRawBytes(Stream src)
		{
			byte[] result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				result = (ImageIO.ToRaw(src, memoryStream) ? memoryStream.ToArray() : null);
			}
			return result;
		}

		// Token: 0x06001BB0 RID: 7088 RVA: 0x00013E85 File Offset: 0x00012085
		public static Texture2D RawToTexture2D(GraphicsDevice graphicsDevice, Stream src)
		{
			return ImageIO.RawToTexture2D(graphicsDevice, new BinaryReader(src, Encoding.UTF8), false);
		}

		// Token: 0x06001BB1 RID: 7089 RVA: 0x004317E8 File Offset: 0x0042F9E8
		public static Texture2D RawToTexture2D(GraphicsDevice graphicsDevice, BinaryReader r, bool async = false)
		{
			int num = r.ReadInt32();
			if (num != 1)
			{
				throw new Exception("Unknown RawImg Format Version: " + num);
			}
			int num2 = r.ReadInt32();
			int num3 = r.ReadInt32();
			byte[] data = r.ReadBytes(num2 * num3 * 4);
			Texture2D texture2D = new Texture2D(graphicsDevice, num2, num3);
			texture2D.SetData<byte>(data);
			return texture2D;
		}

		// Token: 0x06001BB2 RID: 7090 RVA: 0x00431844 File Offset: 0x0042FA44
		public static Task<Texture2D> RawToTexture2DAsync(GraphicsDevice graphicsDevice, BinaryReader r)
		{
			int num = r.ReadInt32();
			if (num != 1)
			{
				throw new Exception("Unknown RawImg Format Version: " + num);
			}
			int width = r.ReadInt32();
			int height = r.ReadInt32();
			byte[] rawdata = r.ReadBytes(width * height * 4);
			return Task.Factory.StartNew<Texture2D>(delegate
			{
				Texture2D texture2D = new Texture2D(graphicsDevice, width, height);
				texture2D.SetData<byte>(rawdata);
				return texture2D;
			});
		}

		// Token: 0x06001BB3 RID: 7091 RVA: 0x004318C8 File Offset: 0x0042FAC8
		public static Task<Texture2D> PngToTexture2DAsync(GraphicsDevice graphicsDevice, Stream stream)
		{
			return Task.Factory.StartNew<Texture2D>(() => Texture2D.FromStream(graphicsDevice, stream));
		}

		// Token: 0x04001893 RID: 6291
		public const int VERSION = 1;
	}
}
